package com.dddd.blog.blogpost.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dddd.blog.api.post.feign.UserVoFeignClient;
import com.dddd.blog.api.post.pojo.Post;
import com.dddd.blog.blogcommon.pojo.vo.PostVO;
import com.dddd.blog.blogpost.mapper.PostMapper;
import com.dddd.blog.blogpost.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dddd
 * @since 2022-02-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    /**
     * 文章数据访问层对象
     */
    @Resource
    private PostMapper postMapper;


    @Autowired
    private UserVoFeignClient userVoFeignClient;

    /** ----------------------------------------------------------*/

    /**
     * 按照当前页和页面条数进行分页获取文章列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    @Cacheable(cacheNames = "post",key = "#currentPage"+"-"+"#pageSize")
    public List<Post> getPostListByPage(long currentPage, long pageSize) {

        Page<Post> postPage = new Page<>(currentPage, pageSize);
        QueryWrapper<Post> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        postMapper.selectPage(postPage,wrapper);
        if(postPage.getRecords() != null){
            return postPage.getRecords();
        }
        return null;
    }

    /**
     * 根据id获取文章信息
     * @param id
     * @return
     */
    @Override
    @Cacheable(cacheNames = "post",key = "#id")
    public PostVO getPostById(Long id) {

//        Post post = postMapper.selectById(id);

//        Result<UserVO> userVO = userVoFeignClient.queryUserVO(post.getAuthorId());

//        post.setAuthorId(userVO.getData().getId());

//        PostVO vo = ObjectInvertUtils.invert(post,PostVO.class);

        PostVO vo = postMapper.getPostById(id);
        return vo;
    }

    @Override
    public Integer getPostCount() {
        QueryWrapper<Post> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        Integer count = postMapper.selectCount(wrapper);
        return count;
    }


    @Override
    public int addPostByMap(Map<String, Object> post) {

        return postMapper.addPostByMap(post);
    }


    @Override
    public int addPostByMapList(List<Map<String, Object>> list) {

        int i = postMapper.addPostByMapList(list);

        return i;
    }

    /**
     * 按阅读排序进行分页
     * @return
     */
    @Override
    public List<Post> getPostOrderByRead() {


        Page<Post> page = new Page<>();
        QueryWrapper<Post>  wrapper = new QueryWrapper<>();
        wrapper.gt("read_count",0)
                .gt("comment_count",0)
                .orderByDesc("read_count","comment_count");
        List<Post> list = postMapper.selectPage(page, wrapper).getRecords();

        return list;
    }

    @Override
    public Integer getPostVisited() {

        QueryWrapper<Post> wrapper = new QueryWrapper<>();

        wrapper.gt("read_count",0)
                .gt("comment_count",0);

        Integer count = postMapper.selectCount(wrapper);

        return count;
    }

    /**
     * 获取阅读量最高的15篇文章
     * @return
     */
    @Override
    public List<Map<String, Object>> getReadRank() {

        QueryWrapper<Post> wrapper = new QueryWrapper();
        wrapper.select("id,title,read_count,comment_count,link")
                .orderByDesc("read_count")
                .orderByDesc("comment_count")
                .last("limit 15");
        return postMapper.selectMaps(wrapper);
    }

    @Override
    public List<Map<String, Object>> getCommentRank() {
        QueryWrapper<Post> wrapper = new QueryWrapper();
        wrapper.select("id,title,read_count,comment_count,link")
                .orderByDesc("comment_count")
                .orderByDesc("read_count")
                .last("limit 15");
        return postMapper.selectMaps(wrapper);
    }


    @Override
    public List<Map<String,Object>> getHotByAuthor(String name) {


        QueryWrapper<Post> wrapper = new QueryWrapper();

        wrapper.select("id,title,link,author_name,read_count,comment_count")
                .eq("author_name",name)
                .orderByDesc("read_count")
                .orderByDesc("comment_count")
                .last("limit 15");

        List<Map<String, Object>> list = postMapper.selectMaps(wrapper);

        return list;
    }
}
